òɾۿûѧϰʹá
ԭַhttps://www.joinquant.com/post/14449

ԭһ˵ʽ鵽ԭĺ߽ۡ


ԭĲԴ£

# ۿ
import jqdata

# ʼ趨ҪĹƱ׼ȵ
def initialize(context):
    # ͨģԤ2017-05-13  2018-03-24̼۸
    context.labels = [3484.4731,3419.8232,3405.0974,3417.4797,3433.4858,3396.5896,3391.5007,3423.003,3421.6697,3465.112,3471.467,3465.7864,3487.3418,3491.7178,3546.959,3436.56,3514.53,3529.1199,3557.9495,3601.758,3541.025,3491.9634,3536.0886,3536.858,3547.974,3528.3457,3542.721,3578.8118,3650.272,3683.9583,3663.6384,3694.049,3699.703,3662.5566,3607.9272,3622.7446,3611.0764,3639.1382,3616.0864,3664.207,3621.2336,3639.712,3707.037,3601.1592,3597.3801,3699.4297,3723.4106,3726.5364,3743.6357,3741.419,3706.606,3683.4072,3748.0212,3733.877,3769.4783,3748.6301,3744.3965,3713.9695,3733.689,3742.2969,3683.7659,3691.0076,3593.5571,3709.6367,3742.3257,3708.5461,3717.597,3745.0974,3761.5957,3754.7583,3776.6123,3761.9675,3735.6716,3799.6272,3764.0454,3800.448,3830.6057,3815.3823,3863.0005,3908.6187,3810.4922,3817.296,3846.0645,3825.2102,3788.061,3874.5283,3854.4138,3820.1802,3850.1833,3858.4153,3857.99,3847.9673,3850.671,3825.7795,3836.8047,3834.4863,3835.9675,3838.4946,3866.0347,3879.392,3895.466,3906.8342,3914.512,3939.9197,3906.9272,3936.1904,3934.7993,3902.4858,3918.806,3951.7837,3966.8457,4007.3047,4015.1685,4024.8362,4025.7075,4017.6519,4003.1348,3986.678,4004.0417,4041.1533,4079.7434,4088.711,4160.403,4160.6655,4145.7754,4104.3496,4112.224,4167.823,4125.745,4262.363,4285.2695,4177.9688,4153.7603,4104.226,4033.8774,4081.047,4040.2146,4026.65,4024.3823,4071.1711,4007.6072,3994.592,4024.8647,4094.205,4038.3335,4040.5596,4036.214,4008.3818,3997.3276,4016.4563,4036.231,4076.2664,4043.742,4079.1006,4068.2737,4041.2944,4060.8088,4032.3176,4079.996,4142.461,4081.8547,4164.163,4211.9136,4179.7275,4208.5596,4199.5635,4216.003,4306.757,4294.753,4307.5894,4276.661,4303.402,4321.5454,4375.543,4401.335,4366.192,4352.632,4368.7324,4271.167,4283.705,4230.268,4257.1523,4299.1123,4228.6143,4157.212,4082.6318,3922.8433,3931.488,3972.2476,3888.1587,4036.5857,4044.2336,4151.31,4116.5684,4052.486,4041.2666,4024.7532,4014.3164,4079.2344,4044.7148,4038.3608,4129.4355,4178.1733,4108.99,4090.9297,4088.3188,4071.6282,4079.1306,4063.0908,4103.768,4068.3186,3979.124,3865.6921,3936.0256,3869.5344,3906.2402,3898.3296,3917.3855,3856.9749,3877.4792,3846.8347,3943.2932,3960.0173,3897.677,3877.7727,3852.6196,3791.1196,3766.4897,3795.0635,3783.1785,3768.4807,3812.6948,3816.6575,3771.9333,3743.5732,3753.6853,3776.6038,3763.3572,3820.007,3878.4482,3826.642,3859.9102,3865.516,3908.1729,3892.5042,3886.5645,3840.99,3892.7214,3937.22,3888.6008,3871.1763,3814.2075,3804.7695,3816.1355,3815.977,3740.9553,3784.1594,3760.4597,3787.7817,3813.8044,3811.6921,3827.662,3775.4116,3760.547,3809.9937,3765.7212,3763.5486,3764.579,3660.5212,3564.6982,3605.7383,3592.9866,3589.3928,3494.2688,3489.3257,3437.5164,3493.3125,3437.318,3355.9753,3408.8022,3466.5505,3404.5852,3445.302,3472.906,3414.7698,3429.539,3479.1172,3484.4563,3474.8198,3472.6768,3462.7024,3408.558,3450.1145,3544.4707,3579.5774,3568.074,3595.3762,3544.1062,3587.7869,3509.163,3462.2117,3410.8735,3377.351,3367.894,3388.9998,3370.033,3433.4487,3389.0122,3431.4001,3385.0483,3356.17,3354.9207,3356.1194,3361.0208,3404.1577,3381.7974,3389.3867,3368.8357,3396.3364,3416.04]
    # һȫֱ, ҪĹƱ
    # 300
    g.security = '399300.XSHE'
    # 趨300Ϊ׼
    set_benchmark('000300.XSHG')
    # ̬Ȩģʽ(ʵ۸)
    set_option('use_real_price', True)
    # к
    run_daily(market_open, time='every_bar')
    # context.labels ݵĶȡλü¼
    context.index  =0
    # 洢һԤֵ
    context.pr_predict_value = 0

# ÿλʱ(ز,ÿһ,,ÿӵһ)һ
def market_open(context):
    security = g.security
    # ȡƱ̼
    close_data = attribute_history(security, 5, '1d', ['close'])
    # ȡһʱ۸
    current_price = close_data['close'][-1]
    # ȡõǰֽ
    cash = context.portfolio.cash
    # ȡԤ̼
    predict_value = context.labels[context.index]
    # ȡ
    error = abs(predict_value-current_price)/current_price
    # һԤֵʼֵ
    if context.pr_predict_value == 0:
        context.pr_predict_value = current_price
    # ǰԤֵ  һԤ̼ 
    if predict_value>context.pr_predict_value:
        order_value(security, cash)
        log.info("Buying %s" % (security))
    else:
    #
        order_target(security, 0)
        log.info("Selling %s" % (security))
    # Ԥ̼۽л
    context.pr_predict_value = predict_value
    # ָһԤ̼۵λ
    context.index = context.index+1
   